const api = require("../../../../ws_micro_shop/utils/api.js");
const tools = require("../../../../ws_micro_shop/utils/getuserinfo.js");
const app = getApp();
const { module: moduleName } = app.siteInfo;
const util = app.util;

Page({
  data: {
    hide: 1,
    qrcode: "",
    unit: "",
    imgUrl: app.imgUrl,
  },
  onLoad(options) {
    let that = this;
    this.setData({
      orgPrice: app.ws_micro_shop_globalData.orgPrice,
    });
    let scene = decodeURIComponent(options.scene);
    let oid = 0;
    if (scene && scene !== "undefined") {
      let scene_obj = scene.split(",");
      oid = scene_obj[0];
      app.ws_micro_shop_globalData.shopid = scene_obj[1];
    }
    if (options && options.shopid) {
      app.ws_micro_shop_globalData.shopid = options.shopid;
    }
    app.tabbarindex =
      options.tabbarindex - 0 ? options.tabbarindex - 0 : app.tabbarindex;
    app.loadCurrencyUnit().then((res) => {
      that.setData({
        unit: res.unit,
      });
    });
    let userinfo = wx.getStorageSync("user_info");
    if (userinfo) {
      Number(userinfo.info) === 0 && tools.onGetInfo(that, userinfo.auth_mode);
      that.getOrderDetails(oid);
      util.prepareBindInfo(options, moduleName, that);
    } else {
      app.ws_micro_shoplogin().then((res) => {
        userinfo = wx.getStorageSync("user_info");
        util.prepareBindInfo(options, moduleName, that);
        Number(userinfo.info) === 0 &&
          tools.onGetInfo(that, userinfo.auth_mode);
        that.getOrderDetails(oid);
      });
    }
  },
  onShareAppMessage() {
    let path =
      "/ws_micro_shop_book/pages/book/clerk/clerk?tabbarindex=" +
      app.tabbarindex +
      "&shopid=" +
      app.ws_micro_shop_globalData.shopid;
    path = util.spliceSharePath(path, "4", moduleName);
    return {
      path,
    };
  },
  onShareTimeline() {
    return {
      title: "",
      imageUrl: "",
      query:
        "tabbarindex=" +
        app.tabbarindex +
        "&shopid=" +
        app.ws_micro_shop_globalData.shopid,
    };
  },
  onHide() {
    util.sendBindInfo(this.data);
  },
  onUnload() {
    util.sendBindInfo(this.data);
  },
  getOrderDetails(id) {
    let that = this;
    wx.showLoading({
      title: "正在加载",
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.book.clerk_order_details,
      method: "GET",
      data: { id },
      success: function (res) {
        if (res.code === 0) {
          that.setData({
            goods: res.data,
          });
        } else {
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                app.util.navigateTo("/ws_micro_shop/pages/user/user", {}, true);
              }
            },
          });
        }
      },
      complete: function (res) {
        setTimeout(function () {
          wx.hideLoading();
        }, 1000);
      },
    });
  },
  goToGoodsDetails(e) {
    app.util.navigateTo(
      "/ws_micro_shop_book/pages/book/details/details",
      { id: this.data.goods.goods_id },
      true
    );
  },
  nowWriteOff(e) {
    let that = this;
    wx.showModal({
      title: "提示",
      content: "是否确认核销？",
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: "正在加载",
            mask: true,
          });
          app.ws_micro_shoprequest({
            url: api.book.clerk,
            data: {
              order_id: that.data.goods.id,
            },
            success: function (res) {
              if (res.code === 0) {
                wx.showModal({
                  showCancel: false,
                  content: "核销成功",
                  confirmText: "确认",
                  success: function (res) {
                    if (res.confirm) {
                      app.util.navigateTo(
                        "/ws_micro_shop/pages/user/user",
                        {},
                        true
                      );
                    }
                  },
                });
              } else {
                wx.showModal({
                  title: "温馨提醒",
                  showCancel: false,
                  content: res.msg,
                  confirmText: "确认",
                  success: function (res) {
                    if (res.confirm) {
                      app.util.navigateTo(
                        "/ws_micro_shop/pages/index/index",
                        {},
                        true
                      );
                    }
                  },
                });
              }
            },
            complete: function () {
              wx.hideLoading();
            },
          });
        }
      },
    });
  },
});
